package com.alibaba.citrus.ots.platform.enhance.functions.dimension.factory;

import com.alibaba.citrus.ots.common.util.SpringContextUtil;
import com.alibaba.citrus.ots.platform.enhance.functions.dimension.entity.DimensionEntity;
import com.alibaba.citrus.ots.platform.enhance.functions.utils.ConverterUtils;
import com.alibaba.citrus.ots.platform.enhance.repository.DimensionQueryRepository;
import com.epoch.app.otsplatformenhance.enums.DimensionTypeEnum;
import com.epoch.app.otsplatformenhance.model.dto.*;
import com.epoch.app.otsplatformenhance.model.enums.StatusEnum;
import com.epoch.app.otsplatformenhance.sdo.DimensionSDO;

import java.util.Objects;

public class DimensionFactory {

    private static DimensionQueryRepository dimensionQueryRepository;

    static {
        dimensionQueryRepository = SpringContextUtil.getBean(DimensionQueryRepository.class);
    }

    public static DimensionEntity deleteDimension(DimensionWriteDeleteRequest request) {
        DimensionSDO dimensionSDO = ConverterUtils.deleteDimensionConverter(request);
        DimensionEntity dimensionEntity = DimensionEntity.of(dimensionSDO);
        return dimensionEntity;
    }

    public static DimensionEntity updateDimension(DimensionWriteUpdateRequest request) {
        DimensionSDO dimensionSDO = dimensionQueryRepository.query(request.getId());
        DimensionSDO dimensionSDO1 = ConverterUtils.updateDimensionConverter(request, dimensionSDO);
        return DimensionEntity.of(dimensionSDO1);
    }

    public static DimensionEntity deleteDimensionStatus(DimensionWriteDeleteRequest request) {
        DimensionSDO dimensionSDO = new DimensionSDO();
        dimensionSDO.setId(Long.parseLong(request.getId()));
        return DimensionEntity.of(dimensionSDO);
    }

    public static DimensionEntity updateStatus(UpdateDimensionStatusRequest request) {
        DimensionSDO dimensionSDO = dimensionQueryRepository.query(request.getId());
        if (Objects.nonNull(request.getStatusDesc())) {
            if (request.getStatusDesc()) {
                dimensionSDO.setStatus(StatusEnum.open.getCode());
            } else {
                dimensionSDO.setStatus(StatusEnum.close.getCode());
            }
        }
        if (Objects.nonNull(request.getExecuteAction())){
            dimensionSDO.setExecuteAction(request.getExecuteAction());
        }

        return DimensionEntity.of(dimensionSDO);
    }

    public static DimensionEntity updatePriority(DimensionSDO dimensionSDO) {
        return DimensionEntity.of(dimensionSDO);
    }

}
